package data

import (
	"github.com/go-redis/redis/extra/redisotel"
	redis "github.com/go-redis/redis/v8"
	"week8/conf"
)

type Data struct {
	Rdb *redis.Client
}

func NewData(config conf.RedisConfig) (*Data, func() error, error) {
	rdb := redis.NewClient(&redis.Options{
		Addr:         config.Addr,
		Password:     config.Password,
		DB:           config.Db,
		DialTimeout:  config.DialTimeout,
		WriteTimeout: config.WriteTimeout,
		ReadTimeout:  config.ReadTimeout,
	})
	rdb.AddHook(redisotel.TracingHook{}) //
	d := &Data{
		Rdb: rdb,
	}

	return d, func() error {
		if err := d.Rdb.Close(); err != nil {
			return err
		}
		return nil
	}, nil
}
